package mil.af.amc.adpetracker.web.actions.AdpeAccount.ajax;

import mil.af.amc.adpetracker.model.AdpeAccount;
import mil.af.amc.adpetracker.model.Account;
import mil.af.amc.adpetracker.model.AccountAdpeAccount;
import mil.af.amc.adpetracker.service.AdpeAccountService;
import mil.af.amc.adpetracker.service.UserService;
import mil.af.amc.adpetracker.service.AccountService;
import mil.af.amc.adpetracker.web.actions.AbstractAction;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;

/**
 * Please Fill in this Javadoc
 *
 * @author TSgt Jason Ferguson
 */

public class GetAdpeAccountsByUserAction extends AbstractAction {

    private static final Log log = LogFactory.getLog(GetAdpeAccountsByUserAction.class);

    private AdpeAccountService adpeAccountService;

    public void setAdpeAccountService(AdpeAccountService adpeAccountService) {
        this.adpeAccountService = adpeAccountService;
    }

    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    private AccountService accountService;

    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }

    @Override
    public ActionForward execute(ActionMapping actionMapping,
                                 ActionForm actionForm,
                                 HttpServletRequest httpServletRequest,
                                 HttpServletResponse httpServletResponse) throws Exception {

        int accountId = Integer.parseInt(httpServletRequest.getParameter("accountId"));
        Account acct = accountService.getById(accountId);

        JSONArray jsonArray = new JSONArray();

        List<AdpeAccount> adpeAccountList = new ArrayList<AdpeAccount>();
        for (AccountAdpeAccount a: acct.getAccountAdpeAccounts()) {
            adpeAccountList.add(a.getAdpeAccount());
        }

        for (AdpeAccount a: adpeAccountList) {
            Map accountMap = new HashMap();
            accountMap.put("id", a.getId());
            accountMap.put("name", a.getName());
            accountMap.put("organization", a.getOrganization());

            JSONObject o = JSONObject.fromObject(accountMap);
            jsonArray.add(o);
        }

        JSONObject j = new JSONObject();
        j.put("results", jsonArray);

        httpServletResponse.setContentType("application/json");
        httpServletResponse.getWriter().println(j);

        return null;
    }
}
